package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.date.DateUtil;
import com.aliyun.gts.bpaas.middleware.cache.api.CacheManager;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.RemindCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2020/12/2 上午11:17
 */
@Service
public class RemindCheckServiceImpl implements RemindCheckService {

    @Autowired(required = false)
    private CacheManager cacheManager;


    @Override
    public boolean setRemindTag(String processInstanceId) {
        if (Objects.nonNull(cacheManager)) {
            cacheManager.set(resolveKey(processInstanceId), "1", 1L, TimeUnit.DAYS);
        }
        return true;
    }

    @Override
    public boolean setRemindTag(String processInstanceId, String taskId) {
        if (Objects.nonNull(cacheManager)) {
            cacheManager.set(resolveKey(processInstanceId, taskId), "1", 1L, TimeUnit.DAYS);
        }
        return true;
    }

    @Override
    public boolean hasRemindTag(String processInstanceId) {
        if (Objects.nonNull(cacheManager)) {
            return cacheManager.hasKey(resolveKey(processInstanceId));
        }
        return false;
    }

    @Override
    public boolean hasRemindTag(String processInstanceId, String taskId) {
        if (Objects.nonNull(cacheManager)) {
            return cacheManager.hasKey(resolveKey(processInstanceId, taskId));
        }
        return false;
    }

    private String resolveKey(String processInstanceId) {
        return processInstanceId + "::" + DateUtil.today();
    }

    private String resolveKey(String processInstanceId, String taskId) {
        return processInstanceId + "::" + taskId + "::" + DateUtil.today();
    }
}
